/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.Component;
import org.aswing.JComboBox;

import com.bourre.data.collections.IndexedArray;

import fever.data.validators.ValidationResources;

import fvaswing.components.forms.FvFormField;

/**
 * Combobox form field.
 * 
 * <p>Choices must be added to field before adding field into a form object.
 * 
 * <p>Example
 * {@code
 *   var combo : FvComboField = new FvComboField( "Your primary pccupation", true );
 *   combo.addChoice( "basket-ball" );
 *   combo.addChoice( "football" );
 *   combo.addChoice( "actionscript" ); *   combo.addChoice( "nothing...too tired" );
 * }
 *  
 * @author Romain Ecarnot
 */
class fvaswing.components.forms.FvComboField extends FvFormField
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _list : IndexedArray;
	private var _input : JComboBox;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 * 
	 * @param label Field's caption
	 */
	public function FvComboField( label : String, required : Boolean )
	{
		super( label );
		
		_isValidationEnabled = ( required == true ) ? true : false;
		
		_list = new IndexedArray();
	}
	
	/**
	 * Sets selected field value.
	 */
	public function setValue( o : Object ) : Void
	{
		super.setValue( o );
	}
	
	/**
	 * Returns selected value.
	 */
	public function getValue( ) : Object
	{
		return  _input.getSelectedItem();
	}
	
	/**
	 * Adds new choice to combobox.
	 * 
	 * <p>Choices must be added to field before adding field into a form object.
	 * 
	 * @param caption combobox data
	 */
	public function addChoice( o : Object ) : Void
	{
		_list.push( o );
	}
	
	/**
	 * Clears field value.
	 */
	public function clear() : Void
	{
		_input.setSelectedIndex( 0 );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Returns component dedicated to field value.
	 */
	private function _createValueComponent() : Component
	{
		_input = new JComboBox( [""].concat( _list ) );
		return _input;
	}
	
	private function _initWithData( o : Object ) : Void
	{
		_input.setSelectedItem( o );	
	}
	
	/**
	 * Process validation
	 */
	private function _validateField() : Boolean
	{
		return _triggerResult( _input.getSelectedIndex() > 0, ValidationResources.getInstance().required_field_error );
	}
}